package com.runen.wnhz.runen.ui.adapter.puadapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.data.entity.CatalogListEntity;
import com.runen.wnhz.runen.data.entity.TestListEntity;
import com.runen.wnhz.runen.widget.GridSpacingItemDecoration;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018-04-26.
 */

public class CatalogAdapter extends RecyclerView.Adapter<CatalogAdapter.ViewHolder> {

    private MyItemClickListener mItemClickListener;
    private LayoutInflater inflater;
    private Context mContext;
    private List<CatalogListEntity> mDatas;
    private boolean isOpen = true;

    public CatalogAdapter(Context mContext, List<CatalogListEntity> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_catalog, parent, false);
        return new ViewHolder(view, mItemClickListener);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CatalogListEntity dataEntity = holder.catalogEntity = mDatas.get(position);
        // 目录名称
        holder.tvName.setText(dataEntity.getTitle());
        CatalogPartAdapter adapter = new CatalogPartAdapter(mContext, dataEntity);
        holder.ryPart.setAdapter(adapter);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private MyItemClickListener mListener;
        CatalogListEntity catalogEntity;

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.iv_open)
        ImageView ivOpen;
        @BindView(R.id.ry_part)
        RecyclerView ryPart;

        public ViewHolder(View itemView, MyItemClickListener onItemClickListener) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mListener = onItemClickListener;
            ivOpen.setOnClickListener(this);
            ryPart.setLayoutManager(new LinearLayoutManager(mContext));
            ryPart.addItemDecoration(new GridSpacingItemDecoration(1,
                    DensityUtil.dp2px(1), false));
        }

        @Override
        public void onClick(View view) {
            if (mListener != null) {
                mListener.onItemClick(view, catalogEntity);
            }
            if (isOpen) {
                ryPart.setVisibility(View.GONE);
                isOpen = false;
                ivOpen.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_down));
            } else {
                ryPart.setVisibility(View.VISIBLE);
                isOpen = true;
                ivOpen.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_up));
            }
        }
    }

    /**
     * 创建一个回调接口
     */
    public interface MyItemClickListener {
        void onItemClick(View view, CatalogListEntity catalogEntity);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
